import Vue from 'vue'
import Vuex from 'vuex'
import Api from '@/utils/api.js';
Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        user: {},
        menus: null,
        roleId: sessionStorage.getItem('roleId'),
        token: sessionStorage.getItem('token'),
        routers:[]
    },
    mutations: {
        setRouters:(state, data) => {
            state.routers = data;
            // sessionStorage.setItem('roleId', data)
        },
        setRoleId:(state, data) => {
            state.roleId = data;
            // sessionStorage.setItem('roleId', data)
        },
        setMenu:(state, data) => {
            state.menuList = data;
            // sessionStorage.setItem('menuList', JSON.stringify(data))
        },
        setToken:(state, data) => {
            state.token = data;
            sessionStorage.setItem('token', data)
        },
        setUserData (state, data) {
            state.user = data.user;
            state.menus = data.menus;
            state.routers = data.routers;
        },
        clearUserInfo (state) {
            state.user = {};
            state.menus = null;
        },
    },
    getters: {
        getToken(state) {
            return state.token;
        }
    },
    actions: {
        getInfo (store) {
            return Api.getInfo().then((res) => {
                store.commit('setUserData', res);
            })
        }
    }
})

export default store;